package com.zgq.share.thirdplatform.sina;

import weibo4android.Weibo;
import weibo4android.WeiboException;
import weibo4android.http.AccessToken;
import weibo4android.http.RequestToken;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.zgq.share.thirdplatform.BasicAdapter;
import com.zgq.share.thirdplatform.IPlatform;
import com.zgq.share.thirdplatform.OAuthToken;
import com.zgq.share.thirdplatform.PlatformDefs;
import com.zgq.share.thirdplatform.Session;
import com.zgq.share.ui.WebViewActivity;

/**
 * 新浪微博分享适配器
 * @author Administrator
 *
 */
public class SinaAdapter extends BasicAdapter implements IPlatform {
	private RequestToken requestToken;
	private AccessToken accessToken;
	
	public SinaAdapter(Context context) {
		super(context);
		System.setProperty("weibo4j.oauth.consumerKey", PlatformDefs.SINA_CONSUMER_KEY);
		System.setProperty("weibo4j.oauth.consumerSecret", PlatformDefs.SINA_CONSUMER_SECRET);
	}
	
	@Override
	public boolean supportLocation() {
		return true;
	}

	@Override
	public boolean supportImage() {
		return true;
	}

	@Override
	public boolean authenticate() {
		Weibo weibo = new Weibo();
		try {
			requestToken = weibo.getOAuthRequestToken(PlatformDefs.CALLBACK_URL);
			Uri uri = Uri.parse(requestToken.getAuthenticationURL() + "&from="
					+ "");
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString("uri", String.valueOf(uri));
			intent.putExtras(bundle);
			intent.setClass(context, WebViewActivity.class);
			context.startActivity(intent);
			
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return false;
	}

	/*@Override
	public boolean send(String title, String message, GeoPointEx point) {
		return false;
	}*/

	@Override
	public boolean validate() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Session parseAuthResult(Intent intent) {
		Session s = new Session();
		OAuthToken token = new OAuthToken();
		Uri uri = intent.getData();
		if (uri == null) {
			return null;
		}
		
		try {
			accessToken = requestToken.getAccessToken(uri
					.getQueryParameter("oauth_verifier"));
			token.setAccessToken(accessToken.getToken());
			token.setAccessTokenSecret(accessToken.getTokenSecret());
			s.setToken(token);
			s.setUid(accessToken.getParameter("user_id"));
		} catch (WeiboException e) {
			e.printStackTrace();
			return null;
		}

		return s;
	}

	@Override
	public Bundle setBundle() {
		Weibo weibo = new Weibo();
		try {
			requestToken = weibo.getOAuthRequestToken(PlatformDefs.CALLBACK_URL);
			Uri uri = Uri.parse(requestToken.getAuthenticationURL() + "&from="
					+ "");
			Bundle bundle = new Bundle();
			bundle.putString("uri", String.valueOf(uri));
			
			return bundle;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
